﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CNNetworking;
using CNUtility;
using BobcatUtility;

namespace CNTestApp
{
    /// <summary>
    /// This dialog shows your address book and lets you connect to another
    /// computer running the application.
    /// 
    /// Recent Connections and Address Book
    /// -----------------------------------
    /// Two sets of connections are shown. Towards the top of the screen we
    /// show the five most recent connections. Below this we show the whole
    /// address book. 
    /// 
    /// </summary>
    public partial class Dialog_NetworkConnection : Form
    {
        // TODO: Add the ability to add contacts to the address book.

        #region Public methods

        public Dialog_NetworkConnection()
        {
            InitializeComponent();
        }

        #endregion

        #region Private functions

        private void Dialog_NetworkConnection_Load(object sender, EventArgs e)
        {
            PropertyBag properties = UserPreferences.getPropertyBag();
            IList<ConnectionSettings> addressBook = properties.getList<ConnectionSettings>("Networking.AddressBook");
            ctrlAllConnections.ConnectionSettings = addressBook;

            //IList<ConnectionSettings> list = new List<ConnectionSettings>();
            //list.Add(new ConnectionSettings { FirstName = "Sophie", LastName = "Wausage", HostName = "COMP1" });
            //list.Add(new ConnectionSettings { FirstName = "Daniel", LastName = "Paniel", HostName = "COMP2" });
            //list.Add(new ConnectionSettings { FirstName = "Joanna", LastName = "Batfish", HostName = "COMP3" });
            //list.Add(new ConnectionSettings { FirstName = "Richard", LastName = "Shepherd", HostName = "COMP4" });
            //ctrlAllConnections.ConnectionSettings = list;
            //ctrlRecentConnections.ConnectionSettings = list;
        }

        /// <summary>
        /// Called when the accept-incoming-connections box is checked. If it is checked, we
        /// start acting as a server and listening for incoming connections.
        /// </summary>
        private void chkAcceptIncomingConnections_CheckedChanged(object sender, EventArgs e)
        {
            if (chkAcceptIncomingConnections.Checked == true)
            {
                NetworkingServer.getInstance().startServer();

                // We connect to ourself to get user-list updates etc, just like 
                // other clients...
                ConnectionSettings serverSettings = NetworkingClient.getInstance().getClientConnectionSettings();
                NetworkingClient.getInstance().connect(serverSettings);
            }
            else
            {
                NetworkingServer.getInstance().stopServer();
            }
        }

        /// <summary>
        /// Called when Connect is pressed.
        /// </summary>
        private void cmdConnect_Click(object sender, EventArgs e)
        {
            // For the moment, we connect to the selected item in the main
            // address book.
            // TODO: Use the most-recent list as well. And update it appropriately.
            ConnectionSettings address = ctrlAllConnections.SelectedItem;
            NetworkingClient.getInstance().connect(address);
        }

        #endregion
    }
}
